package com.smritimap.demo;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.core.widget.NestedScrollView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.CustomViewTarget;
import com.bumptech.glide.request.transition.Transition;
import com.ymxk.timestar.view.smritimap.OnDrawablePopupListener;
import com.ymxk.timestar.view.smritimap.SmritiEntity;
import com.ymxk.timestar.view.smritimap.SmritiMapView;

import java.util.ArrayList;
import java.util.List;

public class SmritiTreeActivity extends AppCompatActivity {

    private SmritiMapView smritiMapView;
    private final String TAG = SmritiTreeActivity.class.getSimpleName();
    private NestedScrollView mNestedScrollView;

    public static void jumpTo(Context context) {
        Intent intent = new Intent(context, SmritiTreeActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initViews(null);

        mNestedScrollView.post(this::onFullScroll);

    }

    private void onFullScroll() {
        if (smritiMapView.isScrollToBottom()) {
            smritiMapView.post(() -> {
                mNestedScrollView.fullScroll(View.FOCUS_DOWN);
            });
        }
    }

    public boolean isApplyStatusBarTranslucency() {
        return true;
    }

    protected int getLayoutId() {
        return R.layout.activity_smriti_tree;
    }

    protected void initViews(View containerView) {

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        smritiMapView = findViewById(R.id.smritiMapView);
        mNestedScrollView = findViewById(R.id.mNestedScrollView);
        smritiMapView.setOnDrawablePopupListener((popupType, entity) -> {
            //星球点击事件
            if (popupType == OnDrawablePopupListener.POPUP_STAR) {
                Toast.makeText(this, "星球主页--》", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, entity.getStarName(), Toast.LENGTH_SHORT).show();
            }
        });


        onSetDrawableLoadListener();

        onLoadLocalData();


        //testGlide();

    }

    private void onSetDrawableLoadListener() {
        smritiMapView.setOnDrawableLoadListener((entity, drawing) -> {
            Log.e(TAG, "onDrawableLoad: ");
            Glide.with(SmritiTreeActivity.this)
                    .asDrawable()
                    .load(entity.getResKey())
                    .placeholder(R.mipmap.ic_placeholder_star)
                    .into(new CustomTarget<Drawable>() {
                        @Override
                        public void onLoadFailed(@Nullable Drawable errorDrawable) {
                            Log.e(TAG, "onLoadFailed: ");
                        }

                        @Override
                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                            drawing.onInvalidate(entity, resource);
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {

                        }
                    });
        });

    }

    private void testGlide() {
        for (int i = 0; i < list.size(); i++) {
            SmritiEntity entity = list.get(i);
            Glide.with(SmritiTreeActivity.this)
                    .asDrawable()
                    .load(entity.getResKey())
                    .into(new CustomViewTarget<View, Drawable>(smritiMapView) {
                        @Override
                        public void onLoadFailed(@Nullable Drawable errorDrawable) {
                            Log.e(TAG, "onLoadFailed: ");
                        }

                        @Override
                        public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                            Log.e(TAG, "onResourceReady: ");
                            //view.setImageDrawable(resource);
                            //onInvalidateDrawable.onInvalidate(entity, resource);
                        }


                        @Override
                        protected void onResourceCleared(@Nullable Drawable placeholder) {
                            Log.e(TAG, "onResourceCleared: ");
                        }

                    });
        }

    }

    List<SmritiEntity> list = new ArrayList<>();

    private void onLoadLocalData() {
        list.clear();
        StarEntity entity0 = new StarEntity("齐潇潇", R.mipmap.ic_defalut_star);
        entity0.setLesserChild(new StarEntity("周天赫", R.mipmap.ic_defalut_star, childArray[0]));
        entity0.setStarUrl(urlArray[0]);
        list.add(entity0);

        StarEntity entity1 = new StarEntity("父亲", R.mipmap.ic_defalut_star);
        entity1.setLesserChild(new StarEntity("柘瑞堂", R.mipmap.ic_defalut_star, childArray[1]));
        entity1.setStarUrl(urlArray[1]);
        list.add(entity1);

        StarEntity entity2 = new StarEntity("用户名字用户(我)", R.mipmap.ic_defalut_star);
        entity2.setLesserChild(new StarEntity("顾雅晗", R.mipmap.ic_defalut_star, childArray[2]));
        entity2.setLesserParent(new StarEntity("鲍晓笑", R.mipmap.ic_defalut_star, pArray[0]));
        entity2.setStarUrl(urlArray[2]);
        list.add(entity2);

        StarEntity entity3 = new StarEntity("女儿", R.mipmap.ic_defalut_star);
        entity3.setStarUrl(urlArray[3]);
        list.add(entity3);

        StarEntity entity4 = new StarEntity("何佳钰", R.mipmap.ic_defalut_star);
        entity4.setStarUrl(urlArray[4]);
        list.add(entity4);

        smritiMapView.setSmritiEntityList(list);
    }

    String[] urlArray = {
            "https://img0.baidu.com/it/u=2316924685,3028247922&fm=253&fmt=auto&app=138&f=PNG?w=500&h=500",
            "https://oss.res.gomezsz.com/oss-res/time-res/uat/7658d2eeb66345b599d459c39752114e.jpg",
            "https://img1.baidu.com/it/u=367978213,2733375568&fm=253&fmt=auto&app=120&f=PNG?w=500&h=1083",
            "https://img2.baidu.com/it/u=1014498968,559774547&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1083",
            "https://oss.res.gomezsz.com/oss-res/time-res/uat/7f4bb8b634cf431bbc70161204c2af50.jpg",
    };

    String[] childArray = {
            "https://img0.baidu.com/it/u=3384286860,958436636&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500",
            "https://img0.baidu.com/it/u=2820670883,3647156695&fm=253&fmt=auto&app=138&f=JPEG?w=776&h=500",
            "https://img1.baidu.com/it/u=772021480,1347924690&fm=253&fmt=auto?w=1138&h=698",
    };

    String[] pArray = {
            "https://img2.baidu.com/it/u=2364645243,3604268449&fm=253&fmt=auto&app=120&f=JPEG?w=607&h=462",
            "https://img0.baidu.com/it/u=2820670883,3647156695&fm=253&fmt=auto&app=138&f=JPEG?w=776&h=500",
            "https://img1.baidu.com/it/u=772021480,1347924690&fm=253&fmt=auto?w=1138&h=698",
    };
}